RoundManager = {}
local this = RoundManager

local state = RoundState.Begin
local roundCnt = 0
local roundMax = 20
local step = 0
-- 是否有人在播放动作
local isInAction = false
local updateTime = 0
local callbackTime = 2

local sortedActions = {}
local aoyiActions = {}

local roundWaitTime = 0.5
local roundEndWaitTime = 0.5
local waitTimer = Timer.New(RoundManager.ActionWaitEnd, roundWaitTime, 1)

function RoundManager.init()
	state = RoundState.Begin
	roundCnt = 0
	step = 0
	isInAction = false
end

function RoundManager.clear()
	tableUtil.clearTable(sortedActions)
	tableUtil.clearTable(aoyiActions)
end

function RoundManager.update()
	if(state == RoundState.Begin) then
		this.StateBegin()
	elseif(state == RoundState.Update) then
		this.StateUpdate()
	elseif(state == RoundState.End) then
		this.StateEnd()
	elseif(stata == RoundState.WaitEnd) then
		this.StateWaitEnd()
	end
end

function RoundManager.StateBegin()
	-- sort 
	--[[
	local i, j = 1 , 1
	local team, enemy = RoleManager.team, RoleManager.enemy
	for k=1, #team + #enemy do
		if(i > #team) then
			if(not enemy[j]:isDead()) then
				table.insert(sortedActions, enemy[j])
			end
			j = j + 1
		elseif(j > #enemy) then
			if(not team[i]:isDead()) then
				table.insert(sortedActions, team[i])
			end
			i = i + 1
		elseif(i == j) then
			if(not team[i]:isDead()) then
				table.insert(sortedActions, team[i])
			end
			i = i + 1
		else
			if(not enemy[j]:isDead()) then
				table.insert(sortedActions, enemy[j])
			end
			j = j + 1
		end
	end
	]]

	BattleCal.sortActions(sortedActions, RoleManager.team, RoleManager.enemy)
	this.InsertAoyiActionToQueue()

	roundCnt = roundCnt + 1
	step = 0
	isInAction = false

	state = RoundState.Update
end

function RoundManager.StateUpdate()

	this.InsertAoyiActionToQueue()

	if(not isInAction) then
		step = step + 1
		if(step > #sortedActions) then
			state = RoundState.End
		elseif (RoleManager.isAllDead(true) or RoleManager.isAllDead(false)) then
			state = RoundState.End
		else
			if(this.Action()) then
				isInAction = true
			end
		end
		--[[
	else
		updateTime = updateTime + Time.deltaTime
		if(updateTime >= callbackTime) then
			isInAction = false
		end
		]]
	end
end

function RoundManager.StateEnd()
	tableUtil.clearTable(sortedActions)
	
	waitTimer:Reset(RoundManager.WaitTimeToEnd, roundEndWaitTime, 1)
	waitTimer:Start()

	Battle.camera:startDamping(Vector3.New(0,0,0), 0.3)
	Battle.camera:startRecoverScaleByTime(0.3)

	state = RoundState.WaitEnd
end

function RoundManager.StateWaitEnd()
	-- body
end

function RoundManager.WaitTimeToEnd()
	--判断是否有一方全部死亡或者回合数达到限制
	if(roundCnt >= roundMax or RoleManager.isAllDead(true) or RoleManager.isAllDead(false)) then
		Battle.battleScene.state = BattleState.EndShow
	else
		state = RoundState.Begin
	end
end

function RoundManager.InsertAoyiAction(role, actionId)
	local actionElement = {}
	actionElement.speed = {
			 					value = 500,
			 					skills = { actionId },
			 			   }
	actionElement.role = role
	table.insert(aoyiActions, actionElement)
end

function RoundManager.InsertAoyiActionToQueue()
	if(#aoyiActions == 0) then
		return
	end

	local insertIndex = step
	for i,v in ipairs(aoyiActions) do
		table.insert(sortedActions, insertIndex + i, v)
	end
	tableUtil.clearTable(aoyiActions)
end

--[[
function RoundManager.Action()
	sortedActions[step]:play("shoot", false)
	local role = sortedActions[step]
	local target = nil
	if(role.isFriend) then
		target = BattleCal.selectTarget(false)
	else
		target = BattleCal.selectTarget(true)
	end

	local damage = BattleCal.cal(role, target)
	target:gotDamage(damage)

	updateTime = 0
	callbackTime = 2
end
]]

function RoundManager.Action()
	local actionElement = sortedActions[step]
	local role = actionElement.role
	if(role:isDead()) then
		return false
	end
	local skillId = BattleCal.checkSkill(role, actionElement.speed, roundCnt)
	if(skillId ~= nil) then
		--GameLog.Warning("role play action "..role.name.." action id is "..skillId)
		role:playAction(skillId)
		return true
	end 

	return false
end

-- callback for role action
function RoundManager.ActionStart()
	GameLog.Log("RoundManager ActionStart!")
end

function RoundManager.ActionEnd()
	GameLog.Log("RoundManager ActionEnd")
	waitTimer:Reset(RoundManager.ActionWaitEnd, roundWaitTime, 1)
	waitTimer:Start()
end

function RoundManager.ActionWaitEnd()
	isInAction = false
end

--
function RoundManager.RoundCount()
	return roundCnt
end